package com.dudu.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.dudu.dao.BillDao;
import com.dudu.service.IOperationPlatformCustAccountService;
import com.dudu.service.impl.OperationPlatformCustAccountServiceImpl;

import core.CoreService;

/**
 * H5 账单相关控制类
 * @author zx
 *
 */
public class BillController extends CoreService implements IBillController{
	
	public static  Logger logger = Logger.getLogger(BillController.class);
	IOperationPlatformCustAccountService operationPlatformCustService=new OperationPlatformCustAccountServiceImpl();
	private static BillDao service = new BillDao();
	
	public String getRechargeHistory(){  
		String periphery_code=this.getRequest().getParameter("periphery_code");
		String cust_account=this.getRequest().getParameter("cust_account");
		String start_time=this.getRequest().getParameter("start_time");
		String end_time=this.getRequest().getParameter("end_time");
		String page_no=this.getRequest().getParameter("page_no");
		String page_size=this.getRequest().getParameter("page_size");
		String callback = this.getRequest().getParameter("callback");  
		String pay_result="-1";
		String pay_comefrom="all";
		String pay_style="all";
		JSONObject object=operationPlatformCustService.getChargeItems(cust_account, periphery_code, pay_result, pay_comefrom, pay_style, start_time, end_time, page_no, page_size);
		String returndata = "";
	     if(callback != null && callback.length() > 0){
	    	 returndata = callback + "(" + object.toString() + ")";
		 }else{
			 returndata = object.toString();
		 }
	     return returnData( returndata );
	}


	@Override
	public String getBillDetails() {
		HttpServletRequest request=this.getRequest();
		String callback = request.getParameter("callback");
		String accountId = request.getParameter("cust_account");
		String appKey = request.getParameter("app_key");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String user_id = request.getParameter("ext_terminalCode");
		String ability_type = request.getParameter("ability_type");
		JSONArray list = new JSONArray();
		list.addAll(service.queryBillList(getDao(), startTime, endTime, accountId, user_id, ability_type,appKey, "1"));

		String data = list.toString();
		data = callback + "(" + data + ")";
		return returnData(data);
	}
	
	@Override  
	public String getTelBillDetailsBySessionId() {
		String sessionId=this.getRequest().getParameter("sessionId");  
		String accountIdentifer=this.getRequest().getParameter("accountIdentifer");  
		String callback=this.getRequest().getParameter("callback");
		log.info("sessionId=="+sessionId);
		log.info("accountIdentifer=="+accountIdentifer);  
		JSONObject result=service.queryBillDetailsById(getDao(), sessionId, accountIdentifer);
		String re="";
		if(result!=null){
			re=result.toString();
		}
		if(callback!=null&&!callback.equals("")){
		    return returnData(callback + "(" + re + ")");   
		}else{
		     return returnData(re); 
		}
	}

	/**
	 * h5查询语音通知的明细数据：根据voiceid和手机号查询
	 */
	@Override  
	public String getSmsBillDetailsBySessionId() {
		String voiceId=this.getRequest().getParameter("voiceId");    
		String telephone=this.getRequest().getParameter("telephone");   
		String callback=this.getRequest().getParameter("callback");
		log.info("voiceId=="+voiceId);
		log.info("telephone=="+telephone);
		JSONObject result=service.querySmsBillDetailsByPhoneAndVoiceId(getDao(), voiceId, telephone);
		
		JSONObject object = new JSONObject();		
		if(result!=null){
			object.put("result", 0);
			object.put("describe", "");
			object.put("data", result.toString());	
		}else{
			object.put("result", -1);
			object.put("describe", "");
			object.put("data", "");			
		}
		if(callback!=null&&!callback.equals("")){
		    return returnData(callback + "(" + object.toString() + ")");   
		}else{
		     return returnData(object.toString()); 
		}
	}
	
	/**
	 * h5查询两方通话的明细数据，根据手机号查询
	 */
	@Override  
	public String getDialBillDetailsByPhoneNum() {
		String phoneNum=this.getRequest().getParameter("phoneNum");      
		String called=this.getRequest().getParameter("called");      
		String callback=this.getRequest().getParameter("callback");
		log.info("phoneNum=="+phoneNum);
		List<JSONObject> result=service.queryDialBillDetailsByPhoneNum(getDao(), phoneNum,called);
		
		JSONObject object = new JSONObject();		
		if(result!=null){ 
			object.put("result", 0);
			object.put("describe", "");
			object.put("data", result.toString());	
		}else{
			object.put("result", -1);
			object.put("describe", "");
			object.put("data", "");			
		}
		if(callback!=null&&!callback.equals("")){
		    return returnData(callback + "(" + object.toString() + ")");   
		}else{
		     return returnData(object.toString()); 
		}
	}

}
